<?php
declare(strict_types=1);

namespace app\admin\controller;

use app\AdminController;
use think\facade\View;
use app\admin\service\UsersService;
use app\admin\service\ResourceService;
use app\admin\service\ArticleService;
use app\admin\service\ArticleViewsService;
use app\admin\service\RequirementsFormService;
use app\admin\service\OrderService;

/**
 * 首页
 * Class Admin
 * @package app\admin\controller
 */
class Admin extends AdminController
{
    /**
     * 首页
     * @return string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function index()
    {
        // 用户数据
        $usersWeek = UsersService::getUsersWeekLine();
        $usersWeekCreate = UsersService::countUsersWeekCreate();
        $userData['usersWeek'] = $usersWeek;
        $userData['usersWeek']['weekCreate'] = $usersWeekCreate;

        // 社群数据
        $resourceData['countResource'] = ResourceService::countResource();
        $resourceData['yesTodayResource'] = ResourceService::yesTodayResource();
        $resourceData['yesTodayLastResource'] = $resourceData['countResource'] - $resourceData['yesTodayResource'];

        // 涨知识数据
        $articleData['countArticle'] = ArticleService::countArticle();
        $articleData['articleWeek'] = ArticleService::getArticleWeekLine();
        $articleData['countArticleViews'] = ArticleViewsService::countArticleViews();

        // 需求数据
        $requirementsForm['todayRequirementsForm'] = RequirementsFormService::todayRequirementsForm();
        $requirementsForm['countRequirementsForm'] = RequirementsFormService::countRequirementsForm();
        $requirementsFormCate = RequirementsFormService::requirementsFormCate();
        $requirementsForm['requirementsFormCate'] = $requirementsFormCate['requirementsFormCate'];
        $requirementsForm['line_data'] = $requirementsFormCate['line_data'];

        // 订单数据
        $orderData = OrderService::getOrderData();

        View::assign([
            'users_data' => $userData,
            'resource_data' => $resourceData,
            'article_data' => $articleData,
            'requirementsForm' => $requirementsForm,
            'order_data' => $orderData,
        ]);

        return View::fetch();
    }
}